package com.jsyl.lwbk.web.controller.business;

import com.jsyl.lwbk.business.dto.ChildReportDTO;
import com.jsyl.lwbk.business.dto.GetReportListRequest;
import com.jsyl.lwbk.business.dto.response.FindProjectSpeciesResponse;
import com.jsyl.lwbk.business.dto.response.GetReportListResponse;
import com.jsyl.lwbk.business.dto.response.GetReportPdfUrlResponse;
import com.jsyl.lwbk.business.dto.response.JudgeIsEditReportResponse;
import com.jsyl.lwbk.business.handle.RestFullService;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.page.PageDomain;
import com.jsyl.lwbk.common.core.page.TableDataInfo;
import com.jsyl.lwbk.common.core.page.TableSupport;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import com.jsyl.lwbk.system.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Fitch
 * 2021/12/31
 */
@Api(value = "ѧϰ����")
@Controller
@RequestMapping("business/learningReport")
public class LearningEvaluateController extends BaseController {

    @Autowired
    private RestFullService restFullService;

    private String prefix = "business/childEvaluate";
    @Value("${es.url}")
    private String esApi;

    @ApiOperation(value = "��ȡС��ѧϰ����")
    @ApiImplicitParams({@ApiImplicitParam(name = "childId", value = "С��id")})
    @RequiresPermissions("business:evaluateReportFlag:view")
    @GetMapping("/learningReport/{childId}/{editFlag}")
    public String learningReport(@PathVariable("childId") Long childId,
                                 @PathVariable(name = "editFlag",required = false) String editFlag, ModelMap mmap) {
        mmap.put("childId", childId);
        mmap.put("editFlag", editFlag);
        return prefix + "/learningReport";
    }

    @ApiOperation(value = "С��ѵ���ƻ�")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "childId", value = "С��id"),
            @ApiImplicitParam(name = "evaluateNumber", value = "�������")
    })
    @GetMapping("/childTrainingProgram/{childId}/{evaluateNumber}")
    public String childTrainingProgram(@PathVariable("childId") Long childId,@PathVariable("evaluateNumber") String evaluateNumber, ModelMap mmap) {
        mmap.put("childId", childId);
        mmap.put("evaluateNumber", evaluateNumber);
        return prefix + "/childTrainingProgram";
    }

    @ApiOperation(value = "��ȡѧϰ����List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "childId", value = "С��id"),
            @ApiImplicitParam(name = "evaluateNumber", value = "�������"),
            @ApiImplicitParam(name = "beginTime", value = "��ʼʱ��"),
            @ApiImplicitParam(name = "endTime", value = "����ʱ��")
    })
    @PostMapping("/findLearningReportList")
    @ResponseBody
    public TableDataInfo findLearningReportList(ChildReportDTO childReportDTO) {
        GetReportListRequest request = new GetReportListRequest();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        request.setChildId(childReportDTO.getChildId());
        request.setPageNum(pageDomain.getPageNum());
        request.setPageSize(pageDomain.getPageSize());
        GetReportListResponse reportListResponse = restFullService.post(esApi+"/report/listByChild", GetReportListResponse.class,request,getHttpHeaders());
        return getDataTable(reportListResponse.getReportList());
    }


    private Map<String, String> getHttpHeaders() {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, String> headers = new HashMap();
        headers.put("token", user.getToken());
        headers.put("Content-Type", "application/json;charset=utf-8");
        return headers;
    }

    @ApiOperation(value = "ѧϰ�б�")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "childId", value = "С��id"),
            @ApiImplicitParam(name = "reportId", value = "����id")
    })
    @GetMapping("/learningTable/{childId}/{reportId}/{editFlag}")
    public String learningTable(@PathVariable("reportId") String reportId, @PathVariable("childId") String childId,
                                @PathVariable(name = "editFlag",required = false) String editFlag ,ModelMap mmap) {
        FindProjectSpeciesResponse response = restFullService.get(esApi+"/species/list/2/"+reportId, FindProjectSpeciesResponse.class,null,getHttpHeaders());
        mmap.put("reportTableDTO", response.getReport());
        mmap.put("reportList", response.getList());
        mmap.put("reportId", reportId);
        mmap.put("childId", childId);
        mmap.put("editFlag", editFlag);
        return "business/template/2/learningTable";
    }

    @ApiOperation(value = "FindProjectSpeciesResponse")
    @ApiImplicitParams({@ApiImplicitParam(name = "reportId", value = "����id")})
    @GetMapping("/editReport/{reportId}")
    @ResponseBody
    public FindProjectSpeciesResponse editReport(@PathVariable("reportId") String reportId) {
        FindProjectSpeciesResponse response = restFullService.get(esApi+"/species/list/1/"+reportId, FindProjectSpeciesResponse.class,null,getHttpHeaders());
        return response;
    }

    @ApiOperation(value = "JudgeIsEditReportResponse")
    @ApiImplicitParams({@ApiImplicitParam(name = "reportId", value = "����id")})
    @GetMapping("/edit/judge/{reportId}")
    @ResponseBody
    public JudgeIsEditReportResponse editJudge(@PathVariable("reportId") String reportId) {
        JudgeIsEditReportResponse response = restFullService.get(esApi+"/report/edit/judge/"+reportId, JudgeIsEditReportResponse.class,null,getHttpHeaders());
        return response;
    }

    @ApiOperation(value = "��ȡ��Ӧ����PDF��URL")
    @ApiImplicitParams({@ApiImplicitParam(name = "reportId", value = "����id")})
    @GetMapping("/getReportPdfUrl/{reportId}")
    @ResponseBody
    public GetReportPdfUrlResponse getReportPdfUrl(@PathVariable("reportId") String reportId) {
        GetReportPdfUrlResponse response = restFullService.get(esApi+"/report/url/"+reportId, GetReportPdfUrlResponse.class,null,getHttpHeaders());
        return response;
    }
}
